package home.diy;

import home.diy.UI.Mydialog;
import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class AutomationActivity extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		SharedPreferences firstlaunch = getSharedPreferences("firstlaunch",
				MODE_PRIVATE);
		if (firstlaunch == null) {
			int i = 0;
		} else {
			firstlaunch.getBoolean("hello", true);
			int j = 0;
		}
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	/**
	 * Implement the ActionBar described in the menu_gen.xml file
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu_gen, menu);
		return true;
	}

	/*
	 * @Override protected Dialog onCreateDialog(int id) {
	 * 
	 * return dialog; }
	 */

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		FragmentManager fragmentManager;
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.Preference:
			FrameLayout lo = (FrameLayout) findViewById(R.id.Preference_frame);
			LinearLayout.LayoutParams params = (LayoutParams) lo
					.getLayoutParams();
			params.width = 400;
			lo.setLayoutParams(params);
			fragmentManager = getFragmentManager();
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();
			fragmentTransaction.add(R.id.Preference_frame,
					new PreferenceFragment());
			fragmentTransaction.commit();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * function called when text is clicked see the menu.xml Title textview and
	 * the onClick and Clickable variables
	 * 
	 * @param view
	 */

	public void ServerButtonClick(View view) {
		SharedPreferences connectionpref = getSharedPreferences("Alpha2",
				MODE_PRIVATE);
		String ipaddress = connectionpref.getString("ipadress", null);
		if (ipaddress == null) {
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			Mydialog newdia = new Mydialog();
			newdia.show(ft, "dialog");
		}
		/*
		 * Intent intent = new Intent(this, ServeurActivity.class);
		 * startActivity(intent); finish();
		 */
	}

	public void TVButtonClick(View view) {

	}

	public void clickOnParamValid() {
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		Fragment prev = getFragmentManager().findFragmentByTag("dialog");
		if (prev != null) {
			ft.remove(prev);
		}
		ft.addToBackStack(null);
		ft.commit();
	}
}